package com.dgut.finaldemo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.finaldemo.entity.Student;
import com.dgut.finaldemo.entity.StudentHealth;
import com.dgut.finaldemo.mapper.StudentHealthMapper;
import com.dgut.finaldemo.mapper.StudentMapper;
import com.dgut.finaldemo.vo.DataVO;
import com.dgut.finaldemo.vo.StudentHealthVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StudentHealthServiceImpl implements StudentHealthService {

    @Autowired
    private StudentHealthMapper studentHealthMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public DataVO<StudentHealth> findData(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        //分页
        IPage<StudentHealth> healthIPage = new Page<>(page,limit);
        IPage<StudentHealth> result =  studentHealthMapper.selectPage(healthIPage,null);

        dataVO.setCount(result.getTotal()); //Long类型

        List<StudentHealth> studentHealthList = result.getRecords();
        List<StudentHealthVO> studentHealthVOList = new ArrayList<>();
        for (StudentHealth studentHealth : studentHealthList){
            StudentHealthVO studentHealthVO = new StudentHealthVO();
            BeanUtils.copyProperties(studentHealth, studentHealthVO);
            studentHealthVOList.add(studentHealthVO);
        }
        dataVO.setData(studentHealthVOList);
        return dataVO;
    }

    @Override
    public Integer addStudentHealth(StudentHealth studentHealth) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("username",studentHealth.getSusername())
                .eq("name",studentHealth.getSname());
        Student student = studentMapper.selectOne(queryWrapper);
        if(student != null){
            studentHealth.setSid(student.getSid());
            studentHealthMapper.insert(studentHealth);
            return 1;
        }else
            return 0;
    }


    @Override
    public void updateStudentHealth(StudentHealth studentHealth) {
        studentHealthMapper.updateById(studentHealth);
    }

    @Override
    public void deleteStudentHealthByHid(Integer hid) {
        studentHealthMapper.deleteById(hid);
    }

    @Override
    public DataVO<StudentHealth> searchStudentHealth(Integer page, Integer limit,
                                                     StudentHealth studentHealth,
                                                     Float maxTemperature) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        if(studentHealth.getSusername()==null)
            studentHealth.setSusername("");
        if(studentHealth.getSname()==null)
            studentHealth.setSname("");
        if(studentHealth.getDate()==null)
            studentHealth.setDate("");
        if(studentHealth.getArea()==null)
            studentHealth.setArea("");

        //分页
        IPage<StudentHealth> healthIPage = new Page<>(page,limit);
        QueryWrapper<StudentHealth> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like("susername",studentHealth.getSusername())
                .like("sname",studentHealth.getSname())
                .like("Date",studentHealth.getDate())
                .like("Area",studentHealth.getArea());
        if(studentHealth.getHid()!=null){
            queryWrapper
                    .eq("hid",studentHealth.getHid());
        }
        if(studentHealth.getSid()!=null){
            queryWrapper
                    .eq("sid",studentHealth.getSid());
        }
        if(studentHealth.getTemperature()!=null&&maxTemperature!=null){
            queryWrapper
                    .between("temperature",studentHealth.getTemperature(),maxTemperature);
        }else if(studentHealth.getTemperature()!=null&&maxTemperature==null){
            queryWrapper
                    .ge("temperature",studentHealth.getTemperature());
        }else if(studentHealth.getTemperature()==null&&maxTemperature!=null){
            queryWrapper
                    .le("temperature",maxTemperature);
        }
        IPage<StudentHealth> result =  studentHealthMapper.selectPage(healthIPage,queryWrapper);

        dataVO.setCount(result.getTotal()); //Long类型

        List<StudentHealth> studentHealthList = result.getRecords();
        List<StudentHealthVO> studentHealthVOList = new ArrayList<>();
        for (StudentHealth studentHealth1 : studentHealthList){
            StudentHealthVO studentHealthVO = new StudentHealthVO();
            BeanUtils.copyProperties(studentHealth1, studentHealthVO);
            studentHealthVOList.add(studentHealthVO);
        }
        dataVO.setData(studentHealthVOList);
        return dataVO;
    }

    @Override
    public DataVO<StudentHealth> searchStudentHealthByDay(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        System.out.println(date);
        String day = dateFormat.format(date);
        System.out.println(day);

        //分页
        IPage<StudentHealth> healthIPage = new Page<>(page,limit);
        QueryWrapper<StudentHealth> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("date",day);
        IPage<StudentHealth> result =  studentHealthMapper.selectPage(healthIPage,queryWrapper);

        dataVO.setCount(result.getTotal()); //Long类型

        List<StudentHealth> studentHealthList = result.getRecords();
        List<StudentHealthVO> studentHealthVOList = new ArrayList<>();
        for (StudentHealth studentHealth : studentHealthList){
            StudentHealthVO studentHealthVO = new StudentHealthVO();
            BeanUtils.copyProperties(studentHealth, studentHealthVO);
            studentHealthVOList.add(studentHealthVO);
        }
        dataVO.setData(studentHealthVOList);
        return dataVO;
    }

    @Override
    public StudentHealth queryStudentHealthByDate(String username, String date) {
        QueryWrapper<StudentHealth> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("date",date)
                .eq("susername",username);
        return studentHealthMapper.selectOne(queryWrapper);
    }
}
